<?php

/// If you want to log something, you may just write :
///
/// Logger::getInstance()->log("something"); for common logging
/// 	and
/// Logger::getInstance()->error("something"); for error logging 
///

class Logger
{
	private static $instance = NULL;
	private $m_logs = NULL;
	private $m_errs = NULL;

	public static function getInstance ()
	{
		if (null === self::$instance)
			self::$instance = new self();
		return self::$instance;
	}

	/// Just blocking for stupid assholes who want to create new singletone instance
	private function __construct()
	{
		$this->m_logs = @fopen($_SERVER["DOCUMENT_ROOT"]."/logs/log.txt", "a");
		if (! $this->m_logs)
		{
			$this->m_logs = @fopen($_SERVER["DOCUMENT_ROOT"]."/logs/log.txt", "w");
			if (! $this->m_logs )
				die ("Log file creation error, check the rules for /logs directory");
		}
		
		$this->m_errs = @fopen($_SERVER["DOCUMENT_ROOT"]."/logs/errors.txt", "a");
		if (! $this->m_errs)
		{
			$this->m_errs = @fopen($_SERVER["DOCUMENT_ROOT"]."/logs/errors.txt", "w");
			if (! $this->m_errs )
				die ("Error file creation error, check the rules for /logs directory");
		}
	}

	public function clear()
	{
		@fclose($this->m_logs);
		@fclose($this->m_errs);
		$this->m_logs = @fopen($_SERVER["DOCUMENT_ROOT"]."/logs/log.txt", "w");	
		$this->m_errs = @fopen($_SERVER["DOCUMENT_ROOT"]."/logs/errors.txt", "w");			
	}
	
	public function __destruct()
	{
		@fclose($this->m_logs);
		@fclose($this->m_errs);
		unset ($this->m_logs);
		unset ($this->m_errs);
	}

	/// Same blocking for stupid assholes
	private function __clone()
	{
	}

	public function log($logstring)
	{
		if ( is_string($logstring) )
		{
			$timestamp = "[".date("d.m.Y G:i:s")."] ";
			$result = $timestamp.$logstring."\n";
			@fwrite($this->m_logs, $result);
		}
	}

	public function error($errorstring)
	{
		if ( is_string($errorstring) )
		{
			$timestamp = "[".date("d.m.Y G:i:s")."] "; // thanks for this string, i hate to write it)
			$result = $timestamp.$errorstring."\n";
			@fwrite($this->m_errs, $result);
		}
	}
		
};

?>